package com.cskaoyan.service;

import com.cskaoyan.bean.goods.vo.GoodsListFromTopicVO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketTopicExample;
import com.cskaoyan.bean.topic.TopicDate;
import com.cskaoyan.bean.topic.TopicDateById;
import com.cskaoyan.bean.topic.WxTopicDetailVo;
import com.cskaoyan.bean.wx.topic.Vo.WxTopicListOfListVo;
import com.cskaoyan.bean.wx.topic.Vo.WxTopicListVo;
import com.cskaoyan.bean.wx.topic.WXTopicListBo;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * oushao
 * 2022-06-06   19:35
 */

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    GoodsService goodsService;

    @Override
    public TopicDate query(BaseParam param, String title, String subtitle) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        //添加条件
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }

        if (!StringUtils.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }

        criteria.andDeletedEqualTo(false);

        String s = param.getSort() + " " + param.getOrder();
        //增加排序规则
        example.setOrderByClause(s);


        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketTopics);

        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();


        return TopicDate.date(total, pages, param.getLimit(), param.getPage(), marketTopics);
    }

    @Transactional
    @Override
    public MarketTopic insert(MarketTopic marketTopic) {

        Date date = new Date();

        marketTopic.setAddTime(date);

        marketTopic.setDeleted(false);

        marketTopicMapper.insert(marketTopic);

        return marketTopic;
    }

    @Transactional
    @Override
    public MarketTopic update(MarketTopic marketTopic) {

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketTopic.getId());
        Date date = new Date();
        marketTopic.setUpdateTime(date);

        marketTopicMapper.updateByExample(marketTopic, example);

        return marketTopic;
    }

    @Override
    public TopicDateById queryById(String id) {

        TopicDateById topicDateById = new TopicDateById();
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(Integer.parseInt(id));
        topicDateById.setTopic(marketTopic);

        Integer[] goods = marketTopic.getGoods();

        List<GoodsListFromTopicVO> goodsListFromTopicVOS = new ArrayList<>();
        for (Integer good : goods) {
            GoodsListFromTopicVO goodsListFromTopicVO = goodsService.selectbyId(good);
            goodsListFromTopicVOS.add(goodsListFromTopicVO);
        }
        topicDateById.setGoodsList(goodsListFromTopicVOS);

        return topicDateById;
    }

    @Transactional
    @Override
    public void deleteById(MarketTopic marketTopic) {

        marketTopic.setDeleted(true);
        Date date = new Date();
        marketTopic.setUpdateTime(date);
        marketTopicMapper.updateByPrimaryKey(marketTopic);

    }

    /**
     * @Description: \
     * @return:
     * @Author: 蒋镇泽
     * @Date: 2022/6/9 14:14
     */
    @Override
    public WxTopicListVo queryList(WXTopicListBo wxTopicListBo) {

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);


        // 会去获得一些分页信息
        // 上面的查询对应的PageInfo → 把原始查询结果放入到构造方法里
        PageHelper.startPage(wxTopicListBo.getPage(), wxTopicListBo.getLimit());
        PageInfo pageInfo = new PageInfo(marketTopics);
        // total是总的数据量 → 是否等于users.length?不是 → 指的是如果不分页的情况下最多会查询出来多少条记录
        // 按照上面的查询的查询条件执行的select count(*)
        long total = pageInfo.getTotal();
        double i = Math.ceil(total / 10);
        int pages = (int) i;

        ArrayList<WxTopicListOfListVo> wxTopicListOfListVos = new ArrayList<>();

        int temp = 1;
        int end = wxTopicListBo.getLimit() * wxTopicListBo.getPage();
        int start = end - 9;
        for (MarketTopic marketTopic : marketTopics) {
            if (temp >= start && temp <= end) {
                wxTopicListOfListVos.add(new WxTopicListOfListVo(marketTopic.getPicUrl(), marketTopic.getPrice(), marketTopic.getSubtitle(), marketTopic.getId(), marketTopic.getTitle(), marketTopic.getReadCount()));
            }
            temp++;
        }
        WxTopicListVo wxTopicListVo = new WxTopicListVo((int) total, pages, wxTopicListBo.getLimit(), wxTopicListBo.getPage(), wxTopicListOfListVos);
        return wxTopicListVo;
    }

    @Override
    public WxTopicDetailVo queryDetailById(String id) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();

        marketTopicExample.createCriteria().andIdNotEqualTo(Integer.valueOf(id));
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        int total = marketTopics.size();
        int limit = 4;
        int pages = (int) Math.ceil(total / 4) + 1;
        int page = 1;
        WxTopicDetailVo wxTopicDetailVo = new WxTopicDetailVo();
        wxTopicDetailVo.setLimit(limit);
        wxTopicDetailVo.setTotal(total);
        wxTopicDetailVo.setPages(pages);
        wxTopicDetailVo.setPage(page);
        wxTopicDetailVo.setList(marketTopics.subList(0, limit));
        return wxTopicDetailVo;


    }


}
